package com.quicks.beauty.server.web;

import com.quicks.beauty.core.entity.Banners;
import com.quicks.beauty.server.common.AjaxResult;
import com.quicks.beauty.server.service.BannerService;
import com.quicks.beauty.server.vos.SourceFrom;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping(value = "banner")
public class BannerController {

    DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("MM月dd日 (EE)", Locale.CHINA);
    DateTimeFormatter nowPattern = DateTimeFormatter.ofPattern("MM月dd日 ", Locale.CHINA);
    DateTimeFormatter datePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
    DateTimeFormatter dateTimePattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

    @Autowired
    private BannerService bannerService;

    @ApiOperation(value = "首页banner")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public AjaxResult hasUserBill() {
        AjaxResult result = new AjaxResult();
        List<Banners> list = bannerService.getBannerList();
        result.setSuccess(true);
        result.setData(list);
        return result;
    }

    @Autowired
    @Qualifier(value = "redisTemplate")
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping(value = "age/{ch}/{type}/{name}")
    @ResponseBody
    public String countSource(@PathVariable("ch") String ch, @PathVariable("type") String type, @PathVariable("name") String name, HttpServletRequest request) {
        if (name == null || !name.contains(".js")) {
            return null;
        }
        String header = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        Browser browser = userAgent.getBrowser();
        OperatingSystem os = userAgent.getOperatingSystem();
        int id = userAgent.getId();
        ListOperations<String, Object> ofList = redisTemplate.opsForList();
        Boolean expandCh = ofList.getOperations().hasKey("expandCh");
        if (!expandCh) {
            ofList.rightPush("expandCh", "qb-channel:" + ch);
        } else {
            List<Object> objects = ofList.range("expandCh", 0, ofList.size("expandCh"));
            boolean contains = objects.contains("qb-channel:" + ch);
            if (!contains) {
                ofList.rightPush("expandCh", "qb-channel:" + ch);

            }
//            for (Object object : objects) {
//                String obj = (String) object;
//                boolean equals = obj.equals("qb-channel:" + ch);
//                if (!equals) {
//                    ofList.rightPush("expandCh", "qb-channel:" + ch);
//                }
//            }
        }


        HashOperations<String, Object, Object> ofHash = redisTemplate.opsForHash();
        Object o = ofHash.get("qb-channel:" + ch, LocalDateTime.now().format(datePattern));
        if (o == null) {
            List<SourceFrom> list = new ArrayList<>();
            SourceFrom from = new SourceFrom();
            from.setId(id + "").setBrowser(browser.getName())
                    .setCh(ch).setType(type).setDates(LocalDateTime.now().format(dateTimePattern))
                    .setOs(os.getName()).setUserAgent(header);
            list.add(from);
            ofHash.put("qb-channel:" + ch, LocalDateTime.now().format(datePattern), list);
        } else {
            List<SourceFrom> list = (List) o;

            SourceFrom from = new SourceFrom();
            from.setId(id + "").setBrowser(browser.getName())
                    .setCh(ch).setType(type).setDates(LocalDateTime.now().format(dateTimePattern))
                    .setOs(os.getName()).setUserAgent(header);
            list.add(from);
            ofHash.put("qb-channel:" + ch, LocalDateTime.now().format(datePattern), list);
        }
        return "s";
    }
}
